<template>
  <div class="newslist" >
    <div class="container" id="container">
      <ul>
        <li
          v-for="(item, index) in newslist"
          :key="index"
          @click="newsDetail(index)"
        >
          <div class="excerpt">
              <p class="title">{{item.title}}</p>
              <p class="source">{{item.source}}-{{item.className}}
                <span class="time">{{time(index)}}</span>
              </p>
          </div>
        </li>
      </ul>
    </div>
    <div class="space"></div>
  </div>
</template>
<script>
import qs from 'qs'

export default {
  props:['type','page'],
  data() {
    return {
      newslist:[]
    }
  },
  methods:{
    time(index){
      var timeStamp = parseInt(this.newslist[index].postTime)
      var year = new Date(timeStamp).getFullYear()
      var month = new Date(timeStamp).getMonth()+1
      var day = new Date(timeStamp).getDate()
      return `${year}-${month}-${day}`
    },
    newsDetail(id){
      this.$router.push({name:'newsDetail',query:{
        item:this.newslist[id]._id
      }})
    }
  },
  created() {
    //加载数据
    this.$axios({
        method:'post',
        url:'/list',
        data:{ type:this.type,page:1 },
        timeout:30000
      }).then(res=>{
        this.newslist = res.data.data
    }).catch(err=>{
      console.log(err)
    })
  },

  watch: {
    type(){
      this.$axios({
        method:'post',
        url:'/list',
        data:{ type:this.type,page:1 },
        timeout:30000
      }).then(res=>{
        this.newslist = res.data.data
    }).catch(err=>{
      console.log(err)
    })
    }
  },
//   watch: {
//     type:function(newval,oldval){
//       console.log(oldval)
//       console.log(newval)
//       this.$axios({
//         method:'post',
//         url:'/list',
//         data:qs.stringify({ type:this.type,page:2 })
//       }).then(res=>{
//         this.newslist = res.data.data
//     })
//     }
//   },
}
</script>
<style scoped>
  .container ul li{
    height: 100px;
    padding: 10px;
    box-sizing: border-box;
    border-bottom: 1px solid gainsboro;
  }
  .container ul li .excerpt{
    width: 65%;
    height: 100%;
  }
  .container ul li .title{
    display: -webkit-box;
    -webkit-line-clamp:2;
    -webkit-box-orient:vertical;
    overflow: hidden;
    height: 60px;
    font-size: 18px;
    letter-spacing: 1px;
  }
  .container ul li .source{
    font-size: 12px;
    letter-spacing: 1px;
    padding-top: 10px;
    line-height: 22px;
    color: #a5a5a5;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .container ul li .time{
    margin-left: 20px;
  }

  .container>>>.loading{
    width: 100%;
    height: 100px;
    background: greenyellow;
    color: hotpink;
    font-size: 30px;
    text-align: center;
  }
  .space{
    height: 40px;
  }
</style>